<?php

function apiPost($url, $curlPost)
{

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_NOBODY, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($curlPost));
    $returnStr = curl_exec($curl);
    curl_close($curl);

    $returnArr = json_decode($returnStr, TRUE);
    return $returnArr;
}

/* 截取字符串
 * $sourcestr 字符串
 * $cutlength 长度
 */

function cutStr($sourcestr, $cutlength)
{
    $returnstr = '';
    $i = 0;
    $n = 0;
    $str_length = strlen($sourcestr); //字符串的字节数
    $mb_str_length = mb_strlen($sourcestr, 'utf-8');
    while (($n < $cutlength) && ($i <= $str_length)) {
        $temp_str = substr($sourcestr, $i, 1);
        $ascnum = ord($temp_str); //得到字符串中第$i位字符的ascii码
        if ($ascnum >= 224) { //如果ASCII位高与224，
            $returnstr = $returnstr . substr($sourcestr, $i, 3); //根据UTF-8编码规范，将3个连续的字符计为单个字符
            $i = $i + 3; //实际Byte计为3
            $n++; //字串长度计1
        } elseif ($ascnum >= 192) { //如果ASCII位高与192，
            $returnstr = $returnstr . substr($sourcestr, $i, 2); //根据UTF-8编码规范，将2个连续的字符计为单个字符
            $i = $i + 2; //实际Byte计为2
            $n++; //字串长度计1
        } elseif ($ascnum >= 65 && $ascnum <= 90) { //如果是大写字母，
            $returnstr = $returnstr . substr($sourcestr, $i, 1);
            $i = $i + 1; //实际的Byte数仍计1个
            $n++; //但考虑整体美观，大写字母计成一个高位字符
        } else { //其他情况下，包括小写字母和半角标点符号，
            $returnstr = $returnstr . substr($sourcestr, $i, 1);
            $i = $i + 1; //实际的Byte数计1个
            $n = $n + 0.5; //小写字母和半角标点等与半个高位字符宽...
        }
    }
    if ($mb_str_length > $cutlength) {
        $returnstr = $returnstr . '...'; //超过长度时在尾处加上省略号
    }
    return $returnstr;
}

/*
 * 验证验证码
 * $postcode 输入的验证码
 */
function gCkcode($postcode)
{
    $ckcode = gCookie('xq_ckcode');
    if (!$postcode || !$ckcode)
        return FALSE;
    list($t, $n) = explode("_", $ckcode);
    return md5($postcode . $t) == $n;
}

/*
 * 验证验证码
 * $var 输入的验证码
 */
function gCookie($var)
{
    $xq_ckpre = 'xq';
    $ckpre = $xq_ckpre ? substr(md5($xq_ckpre), 8, 6) . '_' : '';

    return isset($_COOKIE[$ckpre . $var]) ? $_COOKIE[$ckpre . $var] : '';
}

/*
 * 记录验证码
 * 
 */

function sCookie($name, $value, $expire = 0, $httponly = true)
{
    $lk_ckpre = 'xq';
    $lk_ckpath = '/';
    $lk_ckdomain = '';
    $timestamp = time();

    switch ($expire) {
        case 0:
            $expire = 0;
            break;
        case 1:
            $expire = $timestamp + 31536000;
            break;
        case -1:
            $expire = $timestamp - 31536000;
            break;
        default:
            $expire += $timestamp;
            break;
    }
    !$lk_ckpath && $lk_ckpath = '/';
    $secure = ($_SERVER['SERVER_PORT'] == '443') ? 1 : 0;
    $ckpre = $lk_ckpre ? substr(md5($lk_ckpre), 8, 6) . '_' : '';

    if (PHP_VERSION >= '5.2.0') {
        return setcookie($ckpre . $name, $value, $expire, $lk_ckpath, $lk_ckdomain, $secure, ($httponly ? 1 : 0));
    } else {
        return setcookie($ckpre . $name, $value, $expire, '/', $lk_ckdomain, $secure);
    }
}

//取得IP
function getIP()
{
    $isagent = TRUE;
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $currentIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
        $currentIP = $_SERVER['HTTP_CLIENT_IP'];
    } else {
        $currentIP = $_SERVER['REMOTE_ADDR'];
        $isagent = FALSE;
    }
    return array((preg_match('~[\d\.]{7,15}~', $currentIP, $match) ? $match[0] : 'unknow'), $isagent);
}

//字符编码
function LKEncode($txt, $key)
{
    srand((double)microtime() * 1000000);
    $encrypt_key = md5(rand(0, 32000));
    $ctr = 0;
    $tmp = '';
    for ($i = 0; $i < strlen($txt); $i++) {
        $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
        $tmp .= $encrypt_key[$ctr] . ($txt[$i] ^ $encrypt_key[$ctr++]);
    }
    return base64_encode(PKey($tmp, $key));
}

//字符解码
function LKDecode($txt, $key)
{
    $txt = PKey(base64_decode($txt), $key);
    $tmp = '';
    for ($i = 0; $i < strlen($txt); $i++) {
        $md5 = $txt[$i];
        $tmp .= $txt[++$i] ^ $md5;
    }
    return $tmp;
}

//加密字符
function PKey($txt, $encrypt_key)
{
    $encrypt_key = md5($encrypt_key);
    $ctr = 0;
    $tmp = '';
    for ($i = 0; $i < strlen($txt); $i++) {
        $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
        $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
    }
    return $tmp;
}

//随机码
function random($length, $type = 1)
{
    $random = '';
    $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $num = '0123456789';
    $all = $str . $num;
    if ($type == 1) {
        $sequece = 'num';
    } elseif ($type == 2) {
        $sequece = 'str';
    } else {
        $sequece = 'all';
    }
    $max = strlen($$sequece) - 1;
    for ($i = 0; $i < $length; $i++) {
        $random .= ${$sequece}{mt_rand(0, $max)};
    }
    return $random;
}

/*
 * 导出表格
 */
function export_csv($filename, $data)
{
    header("Content-type:text/csv");
    header("Content-Disposition:attachment;filename=" . $filename);
    header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
    header('Expires:0');
    header('Pragma:public');
    return $data;
}
